package com.liujie.dhunting.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.liujie.dhunting.entity.Administrator;
import com.liujie.dhunting.mapper.AdministratorMapper;
import com.liujie.dhunting.utils.Result;
import com.liujie.dhunting.utils.ToMD5;
import lombok.Data;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.net.ssl.SSLSession;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Data
class Pack {
    Administrator administrator1;
    Administrator administrator2;
}

@RestController
@RequestMapping("/login")
public class AdministratorController {

    @Resource
    AdministratorMapper administratorMapper;

    @PostMapping("/check")
    public Result<?> login(@RequestBody Administrator administrator) {
        Administrator tmp = administratorMapper.selectById(administrator.getUsername());
        String pw=ToMD5.encrypt2ToMD5(administrator.getPassword());
        if (tmp == null) {
            return Result.error("401", "用户名不存在");
        } else if (!tmp.getPassword().equals(pw)) {
            return Result.error("402", "密码错误");
        }
        return Result.success(tmp);
    }

    @PostMapping("/modify")
    public Result<?> modify(@RequestBody Pack p) {
        Administrator administrator = p.getAdministrator1();
        Administrator newadministrator = p.getAdministrator2();
        Administrator tmp = administratorMapper.selectById(administrator.getUsername());
        if (!tmp.getPassword().equals(administrator.getPassword())) {
            return Result.error("402", "用户密码错误");
        }
        int flag = administratorMapper.updateById(newadministrator);
        if (flag == 1) return Result.success();
        else return Result.error();
    }

    @PostMapping("/test")
    public Result<?> test(@RequestBody Administrator administrator) {
        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest().getSession();

        System.out.println(session.getAttribute("administrator"));
        return Result.success();
    }

    @PostMapping("/register")
    public Result<?> register(@RequestBody Administrator administrator) {
        LambdaQueryWrapper<Administrator> wrapper = Wrappers.<Administrator>lambdaQuery()
                .eq(Administrator::getUsername, administrator.getUsername())
                .or()
                .eq(Administrator::getPhone, administrator.getPhone());
        Administrator ad = administratorMapper.selectOne(wrapper);
        if (ad != null) {
            if (ad.getUsername().equals(administrator.getUsername())) {
                return Result.error("403", "用户名已注册");
            }
            if (ad.getPhone().equals(administrator.getPhone())) {
                return Result.error("404", "手机号已注册");
            }
            return Result.error();
        }else{
            int flag=administratorMapper.insert(administrator);
            if(flag!=0) return Result.success();
            return Result.error();
        }
    }
}